<template>
  <div class="app-container">
    <el-row :gutter="24">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="12">
          <el-form-item label="出货仓库" prop="warehouse">
            <el-input v-model="formData.warehouse" placeholder="请输入出货仓库" clearable readonly :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出货日期" prop="deliveryDate">
            <el-date-picker v-model="formData.deliveryDate" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
              :style="{width: '100%'}" placeholder="请选择出货日期" readonly></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户" prop="field101">
            <el-input v-model="formData.field101" placeholder="请输入客户" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户地址" prop="field103">
            <el-input v-model="formData.field103" placeholder="请输入客户地址" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="联系人" prop="field106">
            <el-input v-model="formData.field106" placeholder="请输入联系人" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="联系电话" prop="field121">
            <el-input v-model="formData.field121" placeholder="请输入联系电话" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户传真" prop="field107">
            <el-input v-model="formData.field107" placeholder="请输入客户传真" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="合同号码" prop="field110">
            <el-input v-model="formData.field110" placeholder="请输入合同号码" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="发票号码" prop="field111">
            <el-input v-model="formData.field111" placeholder="请输入发票号码" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="付款方式" prop="field112">
            <el-select v-model="formData.field112" placeholder="请选择付款方式" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in field112Options" :key="index" :label="item.label"
                :value="item.value" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="包装员" prop="field113">
            <el-input v-model="formData.field113" placeholder="请输入包装员" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="快递信息" prop="field114">
            <el-input v-model="formData.field114" placeholder="请输入快递信息" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="物流单号" prop="field115">
            <el-input v-model="formData.field115" placeholder="请输入物流单号" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="运费" prop="field116">
            <el-input v-model="formData.field116" placeholder="请输入运费" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="单据备注" prop="field109">
            <el-input v-model="formData.field109" type="textarea" placeholder="请输入单据备注"
              :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="创建人" prop="creatorId">
            <template>
              <el-select v-model="formData.creatorId" filterable disabled placeholder="请选择">
                <el-option
                  v-for="user in userList"
                  :key="user.userId"
                  :label="user.nickName"
                  :value="user.userId">
                </el-option>
              </el-select>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业务员" prop="userId">
            <template>
              <el-select v-model="formData.userId" filterable placeholder="请选择">
                <el-option
                  v-for="user in userList"
                  :key="user.userId"
                  :label="user.nickName"
                  :value="user.userId">
                </el-option>
              </el-select>
            </template>
          </el-form-item>
          <!--          <el-form-item label="业务员" prop="field124">-->
          <!--            <el-select v-model="formData.field124" placeholder="请选择业务员" clearable :style="{width: '100%'}">-->
          <!--              <el-option v-for="(item, index) in field124Options" :key="index" :label="item.label"-->
          <!--                         :value="item.value" :disabled="item.disabled"></el-option>-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
        </el-col>
        <el-col :span="24" :xs="24">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['selling:delivery:add']"
              >新增</el-button>
            </el-col>
          </el-row>
          <el-table v-loading="loading" :data="redDeliverySubList" @selection-change="">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="" align="center" prop="id" />
            <el-table-column label="送货单号" align="center" prop="deliveryNumber" />
            <el-table-column label="送货日期" align="center" prop="deliveryDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="客户" align="center" prop="customeId" />
            <el-table-column label="联系人" align="center" prop="contact" />
            <el-table-column label="单据备注" align="center" prop="memo" />
            <el-table-column label="打印状态" align="center" prop="printStatus">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.printStatus"/>
              </template>
            </el-table-column>
            <el-table-column label="审核状态" align="center" prop="auditsStatus">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.auditsStatus"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['selling:delivery:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['selling:delivery:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>

        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        nihao
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { getUserList } from "@/api/selling/delivery";
export default {
  name: "Delivery",
  dicts: ['warehouse', 'sys_yes_no', 'payment'],
  components: {},
  props: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRedDeliverySub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售送货单子表格数据
      redDeliverySubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //
      userList: [],
      formData: {
        id: undefined,
        warehouse: undefined,
        deliveryDate: undefined,
        field101: undefined,
        field103: undefined,
        field106: undefined,
        field121: undefined,
        field107: undefined,
        field110: undefined,
        field111: undefined,
        field112: undefined,
        field113: undefined,
        field114: undefined,
        field115: undefined,
        field116: undefined,
        field109: undefined,
        creatorId: undefined,
        field124: undefined,
        field125: undefined,
        field126: undefined,
        field127: undefined,
      },
      form: {},
      rules: {
        warehouse: [{
          required: true,
          message: '请输入出货仓库',
          trigger: 'blur'
        }],
        deliveryDate: [{
          required: true,
          message: '请选择出货日期',
          trigger: 'change'
        }],
        field101: [{
          required: true,
          message: '请输入客户',
          trigger: 'blur'
        }],
        field103: [],
        field106: [],
        field121: [],
        field107: [],
        field110: [],
        field111: [],
        field112: [],
        field113: [],
        field114: [],
        field115: [],
        field116: [],
        field109: [],
        creatorId: [{
          required: true,
          message: '请输入创建人',
          trigger: 'blur'
        }],
        field124: [{
          required: true,
          message: '请选择业务员',
          trigger: 'change'
        }],
      },
      field112Options: [{
        "label": "选项一",
        "value": 1
      }, {
        "label": "选项二",
        "value": 2
      }],
      field124Options: [{
        "label": "选项一",
        "value": 1
      }, {
        "label": "选项二",
        "value": 2
      }],// 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        folderId: null,
        purchasePrice: null,
        sellingPrice: null,
        referencePrice: null
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    init() {
      if (this.id) {
        this.getList();
      } else {
        this.formData.warehouse = 1
        this.formData.deliveryDate = new Date()
        this.formData.creatorId = this.$store.state.user.id
        this.getUser()
        this.loading = false;
      }
    },
    /** 查询销售送货单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      this.loading = false;
    },
    getUser() {
      getUserList().then(res => {
        this.userList = res.data.map(item => {
          return {
            userId: item.userId,
            nickName: item.nickName
          }
        })
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
      };
      this.redDeliverySubList = [];
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售送货单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDelivery(id).then(response => {
        this.form = response.data;
        this.redDeliverySubList = response.data.redDeliverySubList;
        this.open = true;
        this.title = "修改销售送货单";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除销售送货单编号为"' + ids + '"的数据项？').then(function() {
        return delDelivery(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 复选框选中数据 */
    handleRedDeliverySubSelectionChange(selection) {
      this.checkedRedDeliverySub = selection.map(item => item.index)
    },
    submitForm() {
      console.log(this.$store.state.user)
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        // TODO 提交表单
      })
    },
    resetForm(formName) {
      if (formName) {
        this.$refs[formName].resetFields();
      }else{
        this.$refs['elForm'].resetFields();
      }
    },
  }
}

</script>
<style>
</style>
